package controllers

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

type Base struct {
}

type HttpApiResponseFormat struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

type responseFormat struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data string `json:"data"`
}

/**
 * @Author: yang
 * @Description：错误码
 * @Date: 2021/10/19 16:22
 */
var errorCode = map[int]string{

	/***** 基本错误 start ******/
	10000: "参数错误",
	10001: "请求超时",
	10002: "请求失败",
	10003: "数据为空",
	10004: "数据验证失败",
	/***** 基本错误 end ******/

	/***** Room 路由错误编号 start ******/
	20001: "聊天室信息保存失败",
	20002: "聊天室详情获取失败",
	20003: "聊天室用户创建失败",
	/***** Room 路由错误编号 end ******/
}

/**
 * @Author: yang
 * @Description：错误响应
 * @Date: 2021/10/19 16:22
 */
func (b *Base) BadResponse(code int, data string, context *gin.Context) {
	format := responseFormat{
		Code: 10000,
		Msg:  errorCode[10000],
		Data: data,
	}

	if badMsg, exist := errorCode[code]; exist {
		format.Code = code
		format.Msg = badMsg
	}

	context.JSON(200, format)
}

/**
 * @Author: yang
 * @Description： 成功响应
 * @Date: 2021/10/25 14:29
 */
func (b *Base) SuccessResponse(data interface{}, context *gin.Context) {
	context.JSON(
		http.StatusOK,
		struct {
			HttpApiResponseFormat
		}{
			HttpApiResponseFormat: HttpApiResponseFormat{
				Msg:  "操作成功",
				Code: 0,
				Data: data,
			},
		})
}
